package com.manager.controller;

import com.manager.bean.Dormitory;
import com.manager.bean.view.DormitoryView;
import com.manager.service.DormitoryService;
import com.manager.service.view.DormitoryViewService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

import com.manager.bean.PageInfo;

/**
 * (Dormitory)表控制层
 *
 * @author makejava
 * @since 2021-03-02 20:15:30
 */
@Controller
public class DormitoryController {
    /**
     * 服务对象
     */
    @Resource
    private DormitoryService dormitoryService;

    @Resource
    private DormitoryViewService dormitoryViewServiceService;

    @RequestMapping(value = "/findDormitory")
    public String findDormitory(Model model,Integer Id, String Building, String Manager,Integer pageIndex, Integer pageSize) {
        PageInfo<DormitoryView> pi = dormitoryViewServiceService.findPageInfo(Id,Building,Manager,pageIndex,pageSize);
        model.addAttribute("pi", pi);
        return "dormitory_list";
    }


    @RequestMapping(value = "/exportdormitorylist", method = RequestMethod.POST)
    @ResponseBody
    public List<DormitoryView> exportDormitory() {
        List<DormitoryView> dormitoryList = dormitoryViewServiceService.getAll();
        return dormitoryList;
    }


    @RequestMapping("/deleteDormitory")
    @ResponseBody
    public String deleteDormitory(Integer Id) {
        dormitoryService.deleteById(Id);
        return "dormitory_list";
    }


    @RequestMapping(value = "/addDormitory", method = RequestMethod.POST)
    @ResponseBody
    public String addDormitory(@RequestBody Dormitory dormitory) {
        dormitoryService.insert(dormitory);
        return "dormitory_list";
    }


    @RequestMapping(value = "/updateDormitory", method = RequestMethod.POST)
    public String updateDormitory(Dormitory dormitory) {
        dormitoryService.update(dormitory);
        return "redirect:/findDormitory";
    }


    @RequestMapping("/findDormitoryById")
    public String findDormitoryById(Integer Id, HttpSession session, Model model) {
        Dormitory s = dormitoryService.queryById(Id);
        session.setAttribute("s", s);
        model.addAttribute("s", s);
        return "dormitory_edit";
    }

}
